import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ActivatedRoute } from '@angular/router'
import { ModalService } from '@app/shared/utils'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'

@Component({
  selector: 'app-customer-detail',
  templateUrl: './customer-detail.component.html',
  styleUrls: ['./customer-detail.component.css'],
  providers: [ApiService, QlMessageService, PageService]
})
export class CustomerDetailComponent implements OnInit {
  // 客户编码
  public customerCode: any = ''
  // 客户详情
  public detailData: any = ''
  // 客户持有产品搜索参数
  public productName
  public productCode
  public productCate
  @ViewChild('addInfo', { static: true })
  private addInfoTemp: TemplateRef<any>

  @ViewChild('addTag', { static: true })
  private addTagTemp: TemplateRef<any>

  public valuecate: any = ''
  public valuetype: any = ''

  public tagCatesels = [
    { value: '1', label: '基金' },
    { value: '2', label: '债劵' },
    { value: '2', label: '保险' },
    { value: '2', label: '贷款' },
    { value: '2', label: '理财产品' },
    { value: '2', label: '存款' },
    { value: '3', label: '股票' }
  ]
  public tagTypesels = [{ value: '1', label: '手动', selected: true }]

  public category = [
    { value: '小于10万', label: '小于10万' },
    { value: '10-50万', label: '10-50万' },
    { value: '50-100万', label: '50-100万' },
    { value: '大于100万', label: '大于100万' }
  ]

  public tagTypes = [
    { value: '1', label: '基本属性' },
    { value: '2', label: '交易偏好' },
    { value: '3', label: '价值属性' },
    { value: '4', label: '风险属性' },
    { value: '5', label: '行为特征' },
    { value: '6', label: '信用特征' },
    { value: '7', label: '营销偏好' }
  ]

  // 到期产品
  public duetoProduct = []

  public tradList = []


  public formGroup: FormGroup = this.fb.group({})
  public changeFlag: any = '1'
  // 客户持有产品
  public customerproductdata = []

  public tagarraycate = []

  // 标签列表
  public tagListData = [
    {
      recordno: '9d9850f9-bda4-43de-9943-390caf22c07d',
      tagtype: '02',
      tagname: '新增健康状态标签',
      tagcode: '32434324',
      catalogcode: 'T0010008',
      customername: '刘星1',
      customerno: '0111902127'
    },
    {
      catalogname: '性别',
      recordno: 'M000013244375',
      tagtype: '01',
      tagstatus: '01',
      tagname: '男',
      tagcode: 'Q0001',
      catalogcode: 'T0010001',
      tagexplain: '标签说明-男',
      customername: '刘星1',
      customerno: '0111902127'
    }
  ]

  // 标签特征
  public tagFeatures = [
    {
      level: '1',
      catalogname: '金融特征',
      catalogcode: 'T003',
      status: '1'
    },
    {
      level: '1',
      catalogname: '产品特征',
      catalogcode: 'T002',
      status: '1'
    },
    {
      level: '1',
      catalogname: '基础特征',
      catalogcode: 'T001',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '家庭情况',
      catalogcode: 'T0010009',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '健康状态',
      catalogcode: 'T0010008',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '教育程度',
      catalogcode: 'T0010007',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '城乡标识',
      catalogcode: 'T0010006',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '职业类型',
      catalogcode: 'T0010005',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '所属行业',
      catalogcode: 'T0010004',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '婚姻',
      catalogcode: 'T0010003',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '年龄',
      catalogcode: 'T0010002',
      status: '1'
    },
    {
      pcode: 'T001',
      catalogname: '性别',
      catalogcode: 'T0010001',
      status: '1'
    }
  ]

  //营销活动接口
  public marketActivity = [
    {
        "campaignstate": "01",
        "endtime": 1576060220651,
        "starttime": 1568197820639,
        "campaignname": "平安夜营销活动",
        "recordno": "b5a9ebeb-a691-423e-ad50-464a47e2b4f7",
        "assigntime": 1568197820639,
        "campaignresult": "01",
        "campaignno": "C0000001",
        "customername": "刘星4",
        "customerno": "0111902123"
    }
]

  // 客户资产
  public assetsData = []
  // 产品分布
  public options1 = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      data: ['负债', '资产', '流动性负债', '定期存款', '投资资产', '零售贷款', '现金及现金等价']
    },
    toolbox: {
      show: true,
      feature: {
        mark: { show: true },
        dataView: { show: true, readOnly: false },
        magicType: {
          show: true,
          type: ['pie', 'funnel']
        },
        restore: { show: true },
        saveAsImage: { show: true }
      }
    },
    calculable: false,
    series: [
      {
        name: '资产占比',
        type: 'pie',
        selectedMode: 'single',
        radius: [0, 70],
        // for funnel
        x: '20%',
        width: '40%',
        funnelAlign: 'right',
        max: 1548,
        itemStyle: {
          normal: {
            label: {
              position: 'inner'
            },
            labelLine: {
              show: false
            }
          }
        },
        data: [
          { value: 679, name: '负债' },
          { value: 1548, name: '资产', selected: true }
        ]
      },
      {
        name: '资产占比',
        type: 'pie',
        radius: [100, 140],
        x: '60%',
        width: '35%',
        funnelAlign: 'left',
        max: 1048,
        data: [
          { value: 335, name: '流动性负债' },
          { value: 135, name: '零售贷款' },
          { value: 1310, name: '定期存款' },
          { value: 234, name: '投资资产' },
          { value: 348, name: '现金及现金等价' }
        ]
      }
    ]
  }

  // 推荐产品
  public tjcpdata = []

  public contactData = []

  // 切换
  handle(event) {
    this.changeFlag = event
  }

  constructor(
    private fb: FormBuilder,
    private apiService: ApiService,
    public modal: ModalService,
    public dictService: DictService,
    // public dicpipe:DictPipe,
    public cursorService: CursorService,
    private message: QlMessageService,
    public pageService: PageService,
    private router: Router,
    private store: Store,
    public route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.customerCode = params['code']
      // 用户基础信息
      this.apiService.getcostomerDetaildkList(this.customerCode).subscribe(data => {
        this.detailData = data
      })
      // 互动记录
      this.apiService.getcustomerDetailcontact(this.customerCode).subscribe(data => {
        this.contactData = data
      })
      // 客户参与营销活动记录
      this.apiService.getcustomerMarketActivity(this.customerCode).subscribe(data => {
        // this.contactData = data
      })

      // 客户标签列表查询
      this.cursorService.query().eq('customerno',this.customerCode)
      // const sql = 'select * where customerNo =' + "'*" + this.customerCode + "*'"
      this.queryCustomerTag(this.cursorService)

      // 客户以及目录列表
      this.cursorService.statement = 'select *'
      this.tagFeatures.map(x=>{
        this.tagarraycate.push({
          'value':x.catalogcode,
          'label':x.catalogname
        })
      })
      this.apiService.getTagCate(this.cursorService).subscribe(data => {
        // this.tagFeatures = []
      })

      // 交易列表
      this.apiService.getcustomerTransaction().subscribe(data => {
        this.tradList = data
      })
      // 客户持有产品
      this.apiService.getcustomerProductList(this.customerCode).subscribe(data => {
        this.customerproductdata = data
        // 到期产品
        const timestamp = new Date().getTime()
        data.map(x => {
          console.log('产品时间戳', new Date('x.dueDate').getTime())
          if (new Date(x.dueDate.replace(/-/g, '/')).getTime() - timestamp > 72 * 60 * 60 * 1000) {
          } else {
            this.duetoProduct = data
          }
        })
      })

      // 推荐产品
      // const riskcode =  params['risk']
      const riskcode = 'R1'
      this.apiService.getRecommProduct(riskcode).subscribe(data => {
        this.tjcpdata = data
      })
    })

    //  客户资产接口
    this.apiService.getcustomerAssets(this.customerCode).subscribe(data => {
      this.assetsData = data
    })
  }

  /**
   * 客户标签列表和列表查询
   */
  public queryCustomerTag(cursorService){
    this.apiService.getcustomerTagList(this.cursorService).subscribe(data => {
      // this.tagListData
    })
    this.cursorService.query({reset:true})
  }


  public addInfoFun() {
    this.modal
      .open({
        title: '基本信息补充',
        component: this.addInfoTemp
      })
      .subscribe(() => {
        this.message.success('sucess')
      })
  }

  public addTagFun() {
    this.modal
      .open({
        title: '手动添加标签',
        component: this.addTagTemp
      })
      .subscribe(() => {
        this.message.success('sucess')
      })
  }

  public customerProduct() {
    if (this.productName != '' && this.productName != undefined) {
      this.cursorService.query().eq('productname',this.productName)
    }
    if (this.productCode!= '' && this.productCode != undefined) {
      this.cursorService.query().eq('productcode',this.productCode)
    }
    if (this.productCate != '' && this.productCate != undefined) {
      this.cursorService.query().eq('productcategory',this.productCate)
    }
    this.apiService.getcusProduct(this.cursorService).subscribe(data => {
      this.customerproductdata = data
    })
    this.cursorService.query({reset:true})
  }




  public selectChange(event){
    this.cursorService.query().eq('customerno',this.customerCode);
    this.cursorService.query().eq('catalogcode',event);
    // const sql = 'select * where customerNo =' + "'*" + this.customerCode + "*'" + "and catalogcode ="+"'*"+ event +"*'"
    this.queryCustomerTag(this.cursorService)
    this.cursorService.query({reset:true})
  }
}
